package com.juc.threadPoolExecutor.threadPoolExecutor1;

import java.util.concurrent.SynchronousQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class Run5 {
     /**
      * keepAliveTime 为0
      * keepAliveTime：当线程数量大于corePoolSize值时，有没有超过指定时间内
      * 不从线程池中将空闲线程删除的，如果超过此时间单位，则删除，如果为0则
      * 执行完毕后立即从队列中删除.
      * unit：keepAliveTime参数的时间单位
      */
	
	  /**
	   * 队列使用SynchronousQueue类
	   * 线程数量>corePoolSize
	   * 并且线程数量<=maximumPoolSize
	   * 并且keepAliveTime值为0时的作用是线程执行完毕后立即清除
	 * @throws InterruptedException 
	   */
	public static void main(String[] args) throws InterruptedException {
		Runnable runnable =new Runnable() {
			
			@Override
			public void run() {
				try {
					System.out.println(Thread.currentThread().getName() + " run! " +System.currentTimeMillis());
					Thread.sleep(1000);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}			
			}
		};
		ThreadPoolExecutor executor = new ThreadPoolExecutor(7, 10, 0L, TimeUnit.MILLISECONDS, new SynchronousQueue<Runnable>());
		executor.execute(runnable);//1
		executor.execute(runnable);//2
		executor.execute(runnable);//3
		executor.execute(runnable);//4
		executor.execute(runnable);//5
		executor.execute(runnable);//6
		executor.execute(runnable);//7
		executor.execute(runnable);//8
		executor.execute(runnable);//9
		Thread.sleep(300);
		System.out.println("A: " +executor.getCorePoolSize());
		System.out.println("A: " +executor.getPoolSize());
		System.out.println("A: " +executor.getQueue().size());
		Thread.sleep(10000);
		System.out.println("B: " +executor.getCorePoolSize());
	    System.out.println("B: " +executor.getPoolSize());
		System.out.println("B: " +executor.getQueue().size());
	}
}
